package com.dragon.modules.yuegou.service.impl.busd;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.web.PageRequest;
import com.dragon.modules.yuegou.entity.busd.DistrictServiceEntity;
import com.dragon.modules.yuegou.enums.busd.DistrictServiceStatus;
import com.dragon.modules.yuegou.enums.code.BusinessCode;
import com.dragon.modules.yuegou.mapper.busd.DistrictServiceMapper;
import com.dragon.modules.yuegou.model.busd.DistrictService;
import com.dragon.modules.yuegou.query.busd.DistrictServiceQuery;
import com.dragon.modules.yuegou.service.busd.DistrictServiceService;
import com.dragon.modules.yuegou.web.Check;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 商圈服务表 服务实现
 *
 * @author pbuilder generated
 * @date 2019-09-06 14:31:40
 * @version 2.0
 */
@Service("busdDistrictServiceService")
public class DistrictServiceServiceImpl
		extends ServiceImpl<DistrictServiceMapper, DistrictServiceEntity>
		implements DistrictServiceService {
	
	@Override
	public void deleteDistrictServiceById(String id) {
		removeById(id);
	}
	
	@Override
	public IPage<DistrictService> search(PageRequest page, DistrictServiceQuery query) {
		Page<DistrictServiceEntity> pa = new Page<>(page.getPage(), page.getSize(), true);
		QueryWrapper<DistrictServiceEntity> wrapper = query.toQueryWrapper();

		wrapper.orderByAsc(DistrictServiceEntity.COL_SORT_NO);
		wrapper.orderByDesc(DistrictServiceEntity.COL_MODIFIED_TIME);
		IPage<DistrictServiceEntity> iPage = baseMapper.selectPage(pa,wrapper);
		return iPage.convert(DistrictService::new);
		
	}
			
	@Override
    public void upperService(String id) {
		DistrictService districtService = new DistrictService();
		districtService.setId(id);
		districtService.setStatus(DistrictServiceStatus.UPPER);
		baseMapper.updateById(districtService);
		
	}
	
	@Override
	public void saveService(DistrictService districtService) {
		Check.notEmpty(districtService.getBusdId(), BusinessCode.BUSDID_NOTNULL.getMessage());
		Check.notEmpty(districtService.getServiceName(),"服务名不能为空");
		Check.notEmpty(districtService.getHotLine(), "服务热线不能为空");
		Check.notEmpty(districtService.getPosation(), "服务位置热线不能为空");
		
		if(!StringUtils.isEmpty(districtService.getBusdId())) {
			save(districtService);
		}
	}
	
	@Override
    public void lowerService(String id) {
		DistrictService districtService = new DistrictService();
		districtService.setId(id);
		districtService.setStatus(DistrictServiceStatus.LOWER);
		baseMapper.updateById(districtService);
		
	}

	@Override
	public IPage<DistrictService> pageList(PageRequest page, String name, String busdId) {
		Check.notEmpty(busdId, BusinessCode.BUSDID_NOTNULL.getMessage());
		Page<DistrictServiceEntity> pa = new Page<>(page.getPage(), page.getSize(), true);

		DistrictServiceQuery query = new DistrictServiceQuery();
		query.setBusdId(busdId);
		query.serviceName().like(name);
		query.setDel(false);
		QueryWrapper<DistrictServiceEntity> wrapper = query.toQueryWrapper();

		wrapper.orderByAsc(DistrictServiceEntity.COL_SORT_NO);
		wrapper.orderByDesc(DistrictServiceEntity.COL_MODIFIED_TIME);

		IPage<DistrictServiceEntity> iPage = baseMapper.selectPage(pa,wrapper);
		return iPage.convert(DistrictService::new);
	}

	@Override
	public DistrictService getBudsService(String id) {
		Check.notEmpty(id, "服务ID不能为空");
		DistrictServiceEntity entity = baseMapper.selectById(id);
		return new DistrictService(entity);
	}

	@Override
	public void saveBusdService(DistrictService districtService) {
		Check.notEmpty(districtService.getBusdId(), BusinessCode.BUSDID_NOTNULL.getMessage());
		Check.notEmpty(districtService.getIntroduction(),"服务详情不能为空");
		Check.notEmpty(districtService.getServiceName(),"服务名不能为空");
		Check.notEmpty(districtService.getHotLine(), "服务热线不能为空");
		Check.notEmpty(districtService.getPosation(), "服务位置热线不能为空");
		Check.notEmpty(districtService.getLogo(), "logo图片不能为空");
		Check.notNull(districtService.getSortNo(), "序号不能为空");
		
		if (StringUtils.isEmpty(districtService.getId())) {
			baseMapper.insert(districtService);
		}else {
			baseMapper.updateById(districtService);
		}
	}

	@Override
	public void upLoBusdService(String id, DistrictServiceStatus status) {
		Check.notEmpty(id, BusinessCode.BUS_GROUP_SERVICE_ID_IS_NULL.getMessage());
		Check.notNull(status, "商圈服务上下架状态不能空");
		
		//查询商圈服务信息
		DistrictServiceEntity districtService = getById(id);
		//修改服务上下架状态
		if (districtService != null) {
			districtService.setStatus(status);
			baseMapper.updateById(districtService);
		}
	}

	@Override
	public void sortNoBusdService(String id, Integer sortNo) {
		Check.notEmpty(id, BusinessCode.BUS_GROUP_SERVICE_ID_IS_NULL.getMessage());
		Check.notNull(sortNo, "排序不能为空");
		
		//查询商圈服务信息
		DistrictServiceEntity districtService = getById(id);
		//设置序号
		districtService.setSortNo(sortNo);
		baseMapper.updateById(districtService);
		
	}

	@Override
	public void deleteBusdService(String id) {
		Check.notEmpty(id, BusinessCode.BUS_GROUP_SERVICE_ID_IS_NULL.getMessage());
		
		//查询商圈服务信息
		DistrictServiceEntity districtService = getById(id);
		districtService.setDel(true);
		districtService.setDelBy("");
		districtService.setDelTime(LocalDateTime.now());
		baseMapper.updateById(districtService);
		
	}
}